/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.starter.web.framework.flow;

import cn.hallele.infrastructure.metadata.io.Metric;
import cn.hallele.infrastructure.metadata.io.Request;
import cn.hallele.infrastructure.util.NetUtil;
import cn.hallele.ms.starter.web.framework.context.ActionContext;
import cn.hallele.ms.starter.web.framework.node.ServiceFlow;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import lombok.extern.slf4j.Slf4j;

/**
 * hallele_mc_core
 *
 * @author anle5
 * @since 2024/12/23 23:33
 */
@Slf4j
@LiteflowComponent
public class FillRequestFlow extends ServiceFlow {
    @Override
    public void process() throws Exception {
        ActionContext context = this.getContextBean(ActionContext.class);
        fillRequest(context);

        // 服务自定义补充
        context.getActionService().supplementRequest(context);
    }

    private void fillRequest(ActionContext context) {
        Request<?> request = context.getRequest();
        if (request == null) {
            request = Request.builder().build();
        }

        if (request.getMetric() == null) {
            request.setMetric(Metric.builder().build());
        }
        request.getMetric().setRequestId(NetUtil.getRequestId());
        request.setData(this.getRequestData());
    }
}
